package DTO;


import gin.dev.update.DownloadQuere;
import gin.dev.update.DownloadTask;
import gin.dev.update.Installation;

import java.util.ArrayList;

import com.appvn2014.appstorevn.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.provider.Settings.Secure;
import android.view.View;
import anroid.vstore.provider.Constant;
import anroid.vstore.provider.Helper;

public class ProductDTO extends ImageDTO  {

	public static ProductDTO current = null;


	public int id;
	public String name;


	public String dataURL;
	public String description;
	public String filePath;
	public int countDownloads;
	public String publisher;
	public String createdDate;
	public String version;
	public String packageName;
	public ArrayList<String> screenShots = new ArrayList<String>();

	public String sttStr = "";
	public int status = Constant.APP_STT_NOTHING;
	public Boolean isInvalidated = false;

	public String category;


	public String size;

	public static int VerifyAppStatus(Activity context, ProductDTO item)
	{

			PackageInfo pi = Installation.StatusApp(context, item.packageName);
			if(pi != null)

			{
				//				holder.btnViewLocation.setText(R.string.detail_activity_open_app_name);
				item.sttStr = context.getString(R.string.detail_activity_open_app_name);

				(new Thread(new MyRunnable(context, item))).start();

				int versionCode = Integer.parseInt(item.version);
				if(versionCode > pi.versionCode)
				{
					item.status = Constant.APP_STT_UPDATE;
					return Constant.APP_STT_UPDATE;
				}

				item.status = Constant.APP_STT_INSTALLED;
				return Constant.APP_STT_INSTALLED;
			}else  
			{
				int downloadID = DownloadQuere.CheckDownloading(item.packageName);
				if(downloadID != -1)
				{
					DownloadTask task = DownloadQuere.mTaskQuere.get(downloadID);
					if(task.IsFinished() == false)
					{
						item.sttStr = context.getString(R.string.detail_activity_downloading_app);
						item.status = Constant.APP_STT_DOWNLOADING;
						return Constant.APP_STT_DOWNLOADING;

					}
					else
					{
						// if downloading is finished 
						DownloadQuere.ClearTask(task);
						item.sttStr = context.getString(R.string.detail_activity_downloaded_app);
						item.status = Constant.APP_STT_DOWNLOADED;
						return Constant.APP_STT_DOWNLOADED;
						//				holder.btnViewAppStatus.setVisibility(View.VISIBLE);
					}
				}
				else	
				if(Installation.AlreadyDownloaded(context, item.packageName))
				{
					//					holder.btnViewLocation.setText(R.string.detail_activity_install_app);
					item.sttStr = context.getString(R.string.detail_activity_downloading_app);
					item.status = Constant.APP_STT_DOWNLOADED;
					return Constant.APP_STT_DOWNLOADED;
					//				holder.btnViewAppStatus.setVisibility(View.VISIBLE);
				}

				else// not yet down
				{
					//				holder.btnViewLocation.setText(R.string.detail_activity_view_app);
					item.sttStr = context.getString(R.string.detail_activity_view_app);
					item.status = Constant.APP_STT_NOTHING;
					return Constant.APP_STT_NOTHING;
					//				holder.btnViewAppStatus.setVisibility(View.VISIBLE);
				}
			}
		}
	//}





	static class MyRunnable implements Runnable{

		Context context;
		ProductDTO item;
		public MyRunnable(Context context, ProductDTO item)
		{
			this.context = context;
			this.item = item;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			// remove apk after installing
			Installation.DeleteApk(context, item.packageName);

			// post installled success event to server
			String deviceID = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
			Installation.PostInfoInstallSuccess(deviceID, item.id + "");
		}

	}
	Runnable r=new Runnable()
	{
		public void run() 
		{

		}
	};

}
